home *** CD-ROM | disk | FTP | other *** search
- /* Routine to take a 32-bit offscreen and display the luminance delta in an 8-bit
- ** offscreen grayscale.
- ** Note: Both GWorlds must be allocated when this routine is called.
- */
-
- #include "DemoRoutines.h"
-
- void CalculateDeltas( GWorldPtr src, GWorldPtr dst )
- {
- PixMapHandle srcPixMap, dstPixMap;
- short srcRowBytes, dstRowBytes;
- long *srcBaseAddr, *dstBaseAddr, *dstAddr;
- long *srcAddr1;
- short mmuMode;
- short row, column;
- unsigned char lum1,lum2;
- unsigned long dstLong;
- short width, height;
-
- srcPixMap = GetGWorldPixMap ( src );
- dstPixMap = GetGWorldPixMap ( dst );
-
- if ( LockPixels ( srcPixMap ) && LockPixels( dstPixMap) ) /* lock the pixmaps */
- {
- srcBaseAddr = (long *) GetPixBaseAddr ( srcPixMap ); /* get the address of the pixmap */
- srcRowBytes = (**srcPixMap).rowBytes & 0x7fff; /* get the row increment */
- dstBaseAddr = (long *) GetPixBaseAddr ( dstPixMap ); /* get the address of the pixmap */
- dstRowBytes = (**dstPixMap).rowBytes & 0x7fff; /* get the row increment */
- width = (**srcPixMap).bounds.right-(**srcPixMap).bounds.left;
- height = (**srcPixMap).bounds.bottom-(**srcPixMap).bounds.top;
-
- mmuMode = true32b;
- SwapMMUMode ( &mmuMode ); /* set the MMU to 32-bit mode */
- for ( row = 0; row < height; row++ ) /* get each pixel in the pixmap */
- {
- srcAddr1 = srcBaseAddr;
- dstAddr = dstBaseAddr;
- lum1 = CalcLuminance( *srcAddr1++ ); /* calc luminance of src pixel */
- for ( column = 0; column < ((width-1)/4); column++ )
- {
- /*
- ** Do a long in the destination (4 pixels)
- ** This is o.k. since memory blocks are always long word aligned.
- ** Thus, we will never write over the right hand edge.
- */
- dstLong = 0;
- lum2 = CalcLuminance( *srcAddr1++ );
- dstLong = (unsigned char)((0x100 + lum1 - lum2)>>1);
- dstLong = dstLong << 8;
- lum1 = CalcLuminance( *srcAddr1++ );
- dstLong |= (unsigned char)((0x100 + lum2 - lum1)>>1);
- dstLong = dstLong<<8;
- lum2 = CalcLuminance( *srcAddr1++ );
- dstLong |= (unsigned char)((0x100 + lum1 - lum2)>>1);
- dstLong = dstLong << 8;
- lum1 = CalcLuminance( *srcAddr1++ );
- dstLong |= (unsigned char)((0x100 + lum2 - lum1)>>1);
- *dstAddr++ = dstLong;
- }
-
- srcBaseAddr = (long *) ( (char *) srcBaseAddr + srcRowBytes ); /* go to the next row */
- dstBaseAddr = (long *) ( (char *) dstBaseAddr + dstRowBytes ); /* go to the next row */
- }
- SwapMMUMode ( &mmuMode ); /* restore the previous MMU mode */
- UnlockPixels ( srcPixMap ); /* unlock the pixmap */
- UnlockPixels ( dstPixMap ); /* unlock the pixmap */
- }
- }
-
- unsigned char CalcLuminance( long RGBval )
- {
- unsigned char result;
- unsigned char red, green, blue;
-
-
- blue = (char) (RGBval);
- green = (char) (RGBval>>8);
- red = (char) (RGBval>>16);
-
- result = (blue*2 + green*5 + red*9)/16;
- return result;
-
- }